﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System.Collections.Generic;

namespace AbstractClass.Extensions
{
    /// <summary>
    /// Provides a set of extension methods for objects that implements <see cref="IComparer{T}"/>.
    /// </summary>
    public static class ComparerExtensions
    {
        /// <summary>
        /// Reverses the comparison results of <paramref name="comparer"/>.
        /// </summary>
        /// <typeparam name="T">The type of the data to compare.</typeparam>
        /// <param name="comparer">The <see cref="IComparer&lt;T&gt;"/> implementation used to compare objects.</param>
        /// <param name="x">The first object.</param>
        /// <param name="y">The second object.</param>
        /// <returns>
        /// A signed integer that indicates the relative values of x and y, as shown in the following table.
        /// <list type="">
        /// <item>Less than zero : y is less than x</item>
        /// <item>Zero : x equals y</item>
        /// <item>Greater than zero : y is greater than x</item>
        /// </list>
        /// </returns>
        public static int ReverseCompare<T>(this IComparer<T> comparer, T x, T y)
        {
            return comparer.Compare(y, x);
        }

        /// <summary>
        /// Gets a reverse comparer which will reverse the comparison result of <paramref name="comparer"/>.
        /// </summary>
        /// <typeparam name="T">The type of the data to compare.</typeparam>
        /// <param name="comparer">The <see cref="IComparer&lt;T&gt;"/> implementation used to compare objects.</param>
        /// <returns>A reverse comparer object which will reverse the comparison result of <paramref name="comparer"/>.</returns>
        public static IComparer<T> GetReverse<T>(this IComparer<T> comparer)
        {
            return new ReverseComparer<T>(comparer);
        }
    }
}